package wu.example.orderserver.invoker;

import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Directory;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;

import java.util.List;

/**
 * @author benjamin_5
 * @Description
 * @date 2024/8/23
 */
public class MyClusterInvoker<T> extends AbstractClusterInvoker<T> {

    public MyClusterInvoker(Directory<T> directory) {
        super(directory);
    }

    @Override
    protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
        // 使用loadbalance对象选择具体的实例
        System.out.println("执行");
        Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
        try {
            // 执行rpc调用
            return invokeWithContext(invoker, invocation);
        } catch (Throwable e) {
            if (e instanceof RpcException && ((RpcException) e).isBiz()) {
                throw (RpcException) e;
            }

            throw new RpcException(e.getMessage(), e);
        }
    }
}
